<?php
/**
**2015-08-27 对接驴妈妈系统
**User By  PZP
**/
include('/var/www/html/new/d/module/api/LvMaMa/LvMaMa.class.php');
include('/var/www/html/new/d/module/api/function.class.php');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");  
	$le=new go_sql();
	$le->connect();
}

$txt=json_encode($_REQUEST);
 
$lmm = new LvMaMa();
$PFT_infunction = new AllFunction();
$lmm->logfile($txt,'LvMaMa_get_data');

//{"VenueID":"","RoundID":"","AreaID":"","SeatID":"","Tnum":"1","Fid":"50618","PayStatus":"1","TicketID":"23422","TouristName":"\u5f6d\u5fd7\u840d","TouristTel":"15959110796","PlayDate":"2015-09-16 00:00:00","IdCard":"330901197702145911"}
//http://www.12301.cc/module/api/LvMaMa/LvMaMa_get_data.php?TicketID=23422&Fid=50618&PayStatus=1&TouristName=pzp&TouristTel=15959110796&PlayDate=2015-09-26%2000:00:00&IdCard=120101199009281532&Tnum=1
if(!isset($_REQUEST['Action'])){
	
	if(!$_REQUEST['TicketID']) exit("101|0|0|0|0|0");
	
	$tid = $_REQUEST['TicketID'] + 0; //门票id，根据门票id获取航线及票类
	$select_uuid = "select uuid,pid from uu_jq_ticket where id=$tid limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit('101');		
	$uuids = explode("_",$GLOBALS['le']->f('uuid'));  //查询票类id	
	$pid = $GLOBALS['le']->f('pid');  //查询票类id	
	$sellPrice_ldata = $PFT_infunction->Dynamic_Price_And_Storage('124715',$pid,substr($_REQUEST['PlayDate'],0,10),0,"27491");
	if($sellPrice_ldata == 105 || $sellPrice_ldata == 1065) exit('1065|0|0|0|0');
	$sellPrice_ldata_xml =simpleXML_load_string($sellPrice_ldata);
	$sellPrice = (string)$sellPrice_ldata_xml->Rec->UUsprice / 100;
	$name = $_REQUEST['TouristName'];
	$mobile = $_REQUEST['TouristTel'];
	$credentials = $_REQUEST['IdCard'] == 0 ? "" : $_REQUEST['IdCard'];
	// if($_REQUEST['IdCard'] !=0 ){
		for($i=1;$i<=$_REQUEST['Tnum'];$i++){
			$traveller = <<<xml
			<traveller> 
				<name>{$name}</name>
				<enName></enName>
				<mobile>{$mobile}</mobile>
				<email></email>
				<credentials>{$credentials}</credentials>
				<credentialsType>ID_CARD</credentialsType>
				<birthday>2000-01-01</birthday>
			</traveller>
xml;
			$travellers .= $traveller;
		}
	// }	
	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));
	$submit_array = array(
		'productId' 	 => $uuids[0],							// 产品id
		'goodsId'     	 => $uuids[1],							// 票类ID
		'partnerOrderNo' => $ls_order,							// 临时订单号
		'visitDate'      => substr($_REQUEST['PlayDate'],0,10),	 // 日期
		'Ordern'         => $ls_order, 							// 临时订单号
		'quantity'		 => $_REQUEST['Tnum'],					// 数量
		'sellPrice'      => $sellPrice,					 		//销售价
		'orderAmount'	 => $_REQUEST['Tnum'] * $sellPrice,     //总价
		'name'     		 => $name,			// 姓名
		'mobile'		 => $mobile,			//手机
		'credentials'	 => $credentials,				// 身份证
		'travellers'	 => $travellers
	);
	$response = $lmm->validateOrder($submit_array);
	if((string)$response->state->code == 1000){
		if($_REQUEST['PayStatus'] == 1){
			$lmm->orderPayment($submit_array['partnerOrderNo'],(string)$response->order->orderId);
		}
		$lmm->logfile($ls_order.'|'.(string)$response->order->orderId,'createOrder_success');
		exit("200|0|0|0|0|".(string)$response->order->orderId."_".$ls_order);  //无凭证码
	}else{
		$lmm->logfile($submit_array['partnerOrderNo'].'|'.(int)$response->state->code.'|'.(string)$response->state->message,'createOrder_fail');
		exit("101|0|0|0|0|0");
	}
}elseif($_REQUEST['Action'] == 'Relation'){ //下单成功

	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);
	$UUtordernums = explode("_",(string)$pft_order_xml->Rec->UUtordernum);	
	$apiOrder = $UUtordernums[0];
	$tempOrder = $UUtordernums[1];
	$tnum = (int)$pft_order_xml->Rec->UUtnum;

	$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$Ordern','$apiOrder','$tempOrder',1,0,$tnum,'$apiOrder',0,now())";
	$GLOBALS['le']->query($str);
}
elseif($_REQUEST['Action'] == 'Relation_after_pay'){  //散客支付订单
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);		
	$UUtordernums = explode("_",(string)$pft_order_xml->Rec->UUtordernum);	
	$lmm->orderPayment($UUtordernums[1],$UUtordernums[0]);	

}
elseif($_REQUEST['Action'] == 'MOD'){//取消订单
	if($_REQUEST['Tnum'] != 0) exit("1077");  //没有修改的接口			
	$pftOrder = $_REQUEST['Ordern'];
	$selapiOrder = "select tempOrder,apiOrder,handleStatus,oStatus from all_api_order where pftOrder='$pftOrder' limit 1";
	$GLOBALS['le']->query($selapiOrder);
	if(!$GLOBALS['le']->fetch_assoc())	exit('105');
	$apiOrder = $GLOBALS['le']->f('apiOrder');
	$tempOrder = $GLOBALS['le']->f('tempOrder');
	$handleStatus = $GLOBALS['le']->f('handleStatus');
	$oStatus = $GLOBALS['le']->f('oStatus');
	if($oStatus == 3) exit("200");
	if($handleStatus == 0 && $oStatus== 0){
		$orderCancel = $lmm->orderCancel($tempOrder,$apiOrder);
		if($orderCancel == 102) exit("200");
		else exit("1077");
	}
	else exit("1077");
}
?>	else exit("1077");
}
?>